<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %><!-- 数据库操作需要 -->        
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
		//获取参数，转换为相应的类型
		String EmpID=request.getParameter("EmpID");
		String password=request.getParameter("password");
		String phone=request.getParameter("phone");
		String Phone=null;
		String ID=null;
		//第一个java代码段查询数据
		try{
			Class.forName("org.h2.Driver");//加载驱动
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return;
		}
		String url="jdbc:h2:d:/CompanyCar/temp/mydbtest";
		Connection conn=null;

	//数据库连接与查询过程中可能会引发sql异常
	try {
		//获取数据库连接(url,username,password)			
		conn = DriverManager.getConnection(url,"sa","");
		//准备执行数据库插入，此处使用了?方式，比拼接字符串更清晰
		String sql="select EmpID,password from Employee where EmpID='"+EmpID+"'";
		String sql2="update Employee set password=?,phone=? where EmpID='"+EmpID+"'";
		//String sql="insert into Employee(EmpID,password,) values(?,?,?,?,?)";
		Statement stmt=conn.createStatement();
		System.out.println("EmpID="+EmpID);
		//执行数据库查询，获取结果集
		ResultSet rs=stmt.executeQuery(sql);
		//循环获取结果集当中的所有数据
		if (rs.next()){
			ID=rs.getString(1);	//获取第1个字段
			Phone=rs.getString(2);
		}
		stmt.close();
		PreparedStatement pstmt=conn.prepareStatement(sql2);
		if(ID!=null&&Phone==null)
		{
			sql2="update Employee set password=?,phone=?"+"where EmpID="+EmpID;
			//准备SQL执行;注意此处与Statement的不同
			
			pstmt.setString(1,password);
			pstmt.setString(2,phone);
		
			//执行数据库插入，获取结果集
			int result=pstmt.executeUpdate();
			System.out.println("result="+result);
		}
		
		//关闭表达式对象
		pstmt.close();			
		//关闭数据库			
		conn.close();			
		
	} catch (SQLException e) {
		e.printStackTrace();
	} finally{
		//防止数据库未正常关闭
		try{
			if (conn!=null && (!conn.isClosed())){
				conn.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	if(ID==null)
		request.setAttribute("loginok","0");
	else if(Phone!=null)
	request.setAttribute("loginok","-1");
	else
		request.setAttribute("loginok","1");
	request.getRequestDispatcher("/C-form_enroll.jsp").forward(request,response);
%>
</body>
</html>